﻿@{
    ViewBag.Title = "";
}
@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.Common.Enums.Base
@using ZBJF.Warranty.WebProvider
@model List<ZBJF.Warranty.Model.Sellers>
<ul class="nav nav-tabs">
    <li class="Add-R"><a href="/Sellers/">销售商列表</a></li>
    <li class="active"><a href="/Sellers/Edit">新建销售商</a></li>
</ul>
<div style="height: 20px;"></div>
@using (Html.BeginForm("SaveSellers", "Sellers", FormMethod.Post, new { id = "mvcForm", name = "mvcForm", @class = "form-horizontal", enctype = "multipart/form-data" }))
{
    <div class="table-responsive dataTables_wrapper">
        <table id="sellersList" class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th width="20">序号</th>
                    <th width="100">销售商类型</th>
                    <th width="160">销售商系统类型</th>
                    <th>销售商名称</th>
                    <th>销售商代码</th>
                    <th>所属机构</th>
                    <th width="50">操作</th>
                </tr>
            </thead>
            <tbody id="sellersTr">
                @{
    for (int i = 0; i < Model.Count; i++)
    {
        var Sellers = Model.ToList();
        <tr class="text-center" id="trSellers_@(i)">
            @{ var trid = "trSellers_" + i;}
            <td>
                @(i + 1)
                @Html.HiddenFor(m => Sellers[i].ID)
            </td>
            <td>
                @Html.DropDownListFor(m => Sellers[i].SellersType, ConvertToSelector.Instance.SellersTypeToSelectList(), new { validate = "{required:true,messages:{required:'请选择销售商类型！'}}" })
            </td>
            <td>
                @Html.DropDownListFor(m => Sellers[i].SaleSystemType, ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(SaleSystemTypeEnums)), new { validate = "{required:true,messages:{required:'请选择销售商系统类型！'}}" })
            </td>
            <td>
                @Html.TextBoxFor(m => Sellers[i].SellersName, new { maxlength = 20, @class = "input-small w-p95", validate = "{required:true,messages:{required:'请输入销售商名称！'}}", onblur = "VerificationSellersName('Sellers_"+i+"__SellersName')" })
            </td>
            <td>
                @Html.TextBoxFor(m => Sellers[i].SellerCode, "", new { maxlength = 12, @class = "input-small w-p95", validate = "{required:true,messages:{required:'请输入销售机构代码！'}}" })
            </td>
            <td>
                @Html.TextBox("Sellers_" + i + "__OrganizationName", "", new { validate = "{required:true,messages:{required:'请选择所属机构'}}", @readonly = "readonly", @class = "select-icon w-p95", onclick = "showSellersOrganizationLayer('_" + i + "__')" })
                @Html.HiddenFor(m => Sellers[i].SellersOrganizationId)
            </td>
            <td><a onclick='removeSellerItem(this)' style='cursor: pointer' title="删除"><i class='icon-remove'></i></a></td>
        </tr>
    }
                }
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="7">
                        <input id="btnAddNewRow" class="btn btn-primary Edit-R" type="button" value="新增经销商">
                    </td>
                </tr>
            </tfoot>
        </table>
        <div style="text-align: center; margin-top: 100px;">
            <input id="btnAdd" class="btn btn-primary Edit-R" type="submit" value="保存">
            <input id="btnClose" class="btn btn-primary Edit-R" type="reset" value="返回" onclick="javascript: window.location.href = '/Sellers/Index'">
        </div>
    </div>
}
@section scripts{
    <script>
        function VerificationSellersName(obj) {
            if (isExistSellers($("#" + obj).val())) {
                layer.alert("销售商名称已存在，请修改");
            }
        }
        //判断销售商名称是否存在
        function isExistSellers(sellerName) {
            var result = false;
            $.ajax({
                url: "/Sellers/IsExistSellers?sellerName=" + sellerName,
                async: false,
                success: function (data) {
                    result = data == "True";
                }
            });
            return result;
        }
        //增加行
        $("#btnAddNewRow").click(function() {
            var html = $(document.getElementById("sellersList").rows[1]).html();
            var lastTrId = $("#sellersList tbody tr:last-child").attr("id");
            var newId = parseInt(lastTrId.substring(lastTrId.indexOf('_') + 1));
            var regExpOrderItem = new RegExp("0", "g"); //第一行序号都是0
            html = html.replace(regExpOrderItem, newId + 1);

            html = "<tr id='trSellers_" + (newId + 1) + "'>" + html + "</tr>";
            $("#sellersList tbody").append(html);
            $("#sellersList tbody tr:last-child input").val("");
            $("#sellersList tbody tr:last-child td:first").html(newId + 2);
        });

        //删除行
        function removeSellerItem(trInsuranceWarrantyRecordItemId) {
            if (($("#sellersList tbody tr").length == 1)) {
                layer.alert("最后一行不能删除，至少保留一行！");
                return;
            }
            $("#" + trInsuranceWarrantyRecordItemId).remove();
            var i = 0;
            $("#sellersList tbody tr").each(function() {
                $(this).find("td:first").html(i + 1);
                $(this).find("td:last").html("<a class='Edit-R ' href=\"javascript:void(0)\" title='delete' onclick=\"removeSellerItem('trSellers_" + i + "')\"><i class='icon-remove'></i></a>");
                $(this).attr("id", "trSellers_" + i);
                $(this).find("input").each(function() {
                    var regExpID = new RegExp("_\\d\+__", "g"); //替换ID
                    var regExpName = new RegExp("\\[\\d\+\\]", "g"); //替换Name

                    var id = $(this).attr("id");
                    var name = $(this).attr("name");

                    $(this).attr("id", id.replace(regExpID, "_" + i + "__"));
                    $(this).attr("name", name.replace(regExpName, "[" + i + "]"));

                    var clickEvent = $(this).attr("onclick");
                    if (clickEvent && clickEvent != "")
                        $(this).attr("onclick", clickEvent.replace(regExpID, "_" + i + "__"));

                    var onkeyupEvent = $(this).attr("onkeyup");
                    if (onkeyupEvent && onkeyupEvent != "")
                        $(this).attr("onkeyup", onkeyupEvent.replace(regExpID, "_" + i + "__"));
                });
                i++;
            });
        }

        function showSellersOrganizationLayer(i) {
            layer.open({
                type: 2,
                title: '请选所属机构',
                shadeClose: true,
                shade: 0.8,
                area: ['320px', '60%'],
                content: '/PubModelDialog/SellersOrganizationSelector?chkStyle=radio&elementIndex=' + i //单选
            });
        }

        //选中客户后设置值,清空选中的配件
        function setSelectedSellerOrganization(item, i) {
            if (!item.ID) //双击 单个 否则是点击确定多个
                item = item[0];
            if (!item || item == "") {
                $("#Sellers" + i + "SellersOrganizationId").val("");
                $("#Sellers" + i + "OrganizationName").val("");
            } else {
                $("#Sellers" + i + "SellersOrganizationId").val(item.ID);
                $("#Sellers" + i + "OrganizationName").val(item.name);
            }
        }
    </script>
}